<template>
  <el-main>
    <sys-dialog
      :title="sysDialog.title"
      :width="sysDialog.width"
      :height="sysDialog.height"
      :visible="sysDialog.visible"
      @onClose="sysDialogClose"
      @onConfirm="sysDialogConfirm"
    >
      <div slot="content">
        <el-form
          ref="entityForm"
          :model="entityModel"
          :rules="entityRules"
          :inline="true"
          label-width="100px"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item prop="parentId" label="上级菜单">
                <el-tree
                  ref="parentTree"
                  accordion
                  highlight-current
                  default-expand-all
                  :data="parentTreeData"
                  :props="parentTreeProps"
                  node-key="id"
                  class="parentTree"
                  @node-click="parentTreeNodeClick"
                />
              </el-form-item>
              <el-form-item>
                <span style="color:red">* 注意：未选择默认为顶级菜单</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-col :span="24">
                <el-form-item prop="type" label="菜单类型">
                  <el-radio-group v-model="entityModel.type">
                    <el-radio :label="0">目录</el-radio>
                    <el-radio :label="1">菜单</el-radio>
                    <el-radio :label="2">按钮</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-form-item prop="name" label="菜单名称">
                <el-input v-model="entityModel.name" placeholder="请输入菜单名称" />
              </el-form-item>
              <el-form-item prop="code" label="菜单标识">
                <el-input v-model="entityModel.code" placeholder="请输入菜单标识" />
              </el-form-item>
              <el-form-item v-if="entityModel.type === 0 || entityModel.type === 1" prop="icon" label="菜单图标">
                <el-input v-model="entityModel.icon" placeholder="请输入菜单图标" />
              </el-form-item>
              <el-form-item v-if="entityModel.type === 0 || entityModel.type === 1" prop="path" label="路由地址">
                <el-input v-model="entityModel.path" placeholder="请输入路由地址" />
              </el-form-item>
              <el-form-item v-if="entityModel.type === 1" prop="component" label="路由组件">
                <el-input v-model="entityModel.component" placeholder="请输入路由组件" />
              </el-form-item>
              <el-form-item v-if="entityModel.type === 1" prop="hidden" label="侧边栏显示">
                <el-switch v-model="entityModel.hidden" :active-value="false" :inactive-value="true" />
              </el-form-item>
              <el-form-item prop="orderNum" label="显示顺序">
                <el-input-number v-model="entityModel.orderNum" :min="1" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </sys-dialog>
    <el-form
      ref="searchForm"
      :model="searchModel"
      :rules="searchRules"
      :inline="true"
      label-width="80px"
    >
      <el-form-item v-if="checkPermission(['sys:menu:search'])" prop="name">
        <el-input v-model="searchModel.name" placeholder="请输入菜单名称" />
      </el-form-item>
      <el-form-item>
        <el-button v-if="checkPermission(['sys:menu:search'])" icon="el-icon-search" @click="searchHandler()">查询</el-button>
        <el-button v-if="checkPermission(['sys:menu:search'])" icon="el-icon-delete" @click="resethHandler()">重置</el-button>
        <el-button v-if="checkPermission(['sys:menu:add'])" type="primary" icon="el-icon-plus" @click="addHandler()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableList"
      :height="tableHeight"
      row-key="id"
      border
      stripe
      highlight-current-row
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="菜单名称" />
      <el-table-column prop="code" label="菜单标识" />
      <el-table-column prop="type" label="菜单类型" align="center" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 0" type="success">目录</el-tag>
          <el-tag v-if="scope.row.type === 1" type="danger">菜单</el-tag>
          <el-tag v-if="scope.row.type === 2">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="icon" label="菜单图标" align="center" width="100">
        <template slot-scope="scope">
          <!-- Element-UI的图标 -->
          <i v-if="scope.row.icon && scope.row.icon.includes('el-icon')" :class="scope.row.icon" />
          <!-- 阿里巴巴矢量图标 -->
          <svg-icon v-if="scope.row.icon && !scope.row.icon.includes('el-icon')" :icon-class="scope.row.icon" />
        </template>
      </el-table-column>
      <el-table-column prop="path" label="路由地址" />
      <el-table-column prop="component" label="路由组件" />
      <el-table-column prop="hidden" label="侧边栏显示" align="center" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 1 && scope.row.hidden === false" type="success">开启</el-tag>
          <el-tag v-if="scope.row.type === 1 && scope.row.hidden === true" type="danger">关闭</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="orderNum" label="显示顺序" align="center" width="100" />
      <el-table-column prop="createTime" label="创建时间" align="center" />
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="scope">
          <el-button v-if="checkPermission(['sys:menu:update'])" type="primary" icon="el-icon-edit-outline" @click="updateHandler(scope.row)">编辑</el-button>
          <el-button v-if="checkPermission(['sys:menu:delete'])" type="danger" icon="el-icon-close" @click="deleteHandler(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-main>
</template>

<script>
import checkPermission from '@/utils/permission'
import { add, deleteById, update, getById, getList, search, getParent } from '@/api/sysMenu'
import SysDialog from '@/components/SysDialog'
export default {
  name: 'SysMenu',
  components: {
    SysDialog
  },
  props: {},
  data() {
    return {
      loading: false,
      tableList: [],
      tableTotal: 0,
      tableHeight: 0,
      sysDialog: {
        title: '系统提示',
        type: 'add',
        visible: false,
        width: 650,
        height: 480
      },
      entityModel: {
        id: null,
        name: null,
        code: null,
        type: 0,
        icon: null,
        path: null,
        component: null,
        hidden: false,
        parentId: 0,
        orderNum: 1,
        createTime: null
      },
      entityRules: {
        name: [{ required: true, trigger: 'blur', message: '请输入菜单名称' }],
        code: [{ required: true, trigger: 'blur', message: '请输入菜单标识' }],
        icon: [{ required: true, trigger: 'blur', message: '请输入菜单图标' }],
        path: [{ required: true, trigger: 'blur', message: '请输入路由地址' }],
        component: [{ required: true, trigger: 'blur', message: '请输入路由组件' }]
      },
      searchModel: {
        name: null
      },
      searchRules: {
        name: [{ required: true, trigger: 'blur', message: '请输入菜单名称' }]
      },
      parentTreeData: [],
      parentTreeProps: {
        label: 'name',
        children: 'children'
      },
      parentTreeCheckedId: null
    }
  },
  created() {
    this.fetchList()
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 185
    })
  },
  methods: {
    checkPermission,
    fetchList() {
      this.loading = true
      getList().then(response => {
        if (response.code === 200) {
          this.tableList = response.data
          this.loading = false
        }
      })
    },
    sysDialogOpen(title, type) {
      // 设置窗口属性
      this.sysDialog.title = title
      this.sysDialog.type = type
      this.sysDialog.visible = true
      // 重置表单内容
      this.$nextTick(() => {
        this.$refs.entityForm.resetFields()
        this.entityModel.id = null
        this.entityModel.icon = null
        this.entityModel.path = null
        this.entityModel.component = null
      })
      // 获取菜单列表
      getParent().then(response => {
        if (response.code === 200) {
          this.parentTreeData = response.data
        }
      })
    },
    sysDialogClose() {
      this.sysDialog.visible = false
      this.parentTreeCheckedId = null
    },
    sysDialogConfirm() {
      this.$refs.entityForm.validate(valid => {
        if (valid) {
          if (this.sysDialog.type === 'add') {
            add(this.entityModel).then(response => {
              if (response.code === 200) {
                this.$notify.success({ title: '系统提示', message: response.msg })
                this.fetchList()
                this.sysDialogClose()
              }
            })
          }
          if (this.sysDialog.type === 'update') {
            update(this.entityModel).then(response => {
              if (response.code === 200) {
                this.$notify.success({ title: '系统提示', message: response.msg })
                this.fetchList()
                this.sysDialogClose()
              }
            })
          }
        }
      })
    },
    parentTreeNodeClick(data) {
      const lastId = this.parentTreeCheckedId
      const currId = data.id
      if (currId === lastId) {
        this.$refs.parentTree.setCurrentKey(null)
        this.parentTreeCheckedId = null
        this.entityModel.parentId = 0
      } else {
        this.$refs.parentTree.setCurrentKey(currId)
        this.parentTreeCheckedId = currId
        this.entityModel.parentId = currId
      }
    },
    searchHandler() {
      this.$refs.searchForm.validate(valid => {
        if (valid) {
          this.loading = true
          search(this.searchModel).then(response => {
            if (response.code === 200) {
              this.tableList = response.data
              this.loading = false
            }
          })
        }
      })
    },
    resethHandler() {
      this.$refs.searchForm.resetFields()
      this.fetchList()
    },
    addHandler() {
      this.sysDialogOpen('新增系统菜单', 'add')
    },
    updateHandler(row) {
      this.sysDialogOpen('修改系统菜单', 'update')
      getById({ id: row.id }).then(response => {
        if (response.code === 200) {
          this.entityModel = response.data
          if (this.entityModel.parentId !== 0) {
            this.$refs.parentTree.setCurrentKey(this.entityModel.parentId)
            this.parentTreeCheckedId = this.entityModel.parentId
          }
        }
      })
    },
    deleteHandler(row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById({ id: row.id }).then(response => {
          if (response.code === 200) {
            this.$notify.success({ title: '系统提示', message: response.msg })
            this.fetchList()
          }
        })
      }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.parentTree {
  width: 184px;
  border-radius: 4px;
  border: 1px solid #DCDFE6;
}
</style>
